package com.sicnu.demo07

import android.content.Intent
import android.os.Bundle
import android.widget.Button
import android.widget.CheckBox
import android.widget.EditText
import android.widget.RadioButton
import android.widget.RadioGroup
import androidx.appcompat.app.AppCompatActivity

class EditInfoActivity : AppCompatActivity() {
    
    private lateinit var etName: EditText
    private lateinit var etStudentId: EditText
    private lateinit var etPhone: EditText
    private lateinit var etEmail: EditText
    private lateinit var rgGender: RadioGroup
    private lateinit var rbMale: RadioButton
    private lateinit var rbFemale: RadioButton
    private lateinit var cbTravel: CheckBox
    private lateinit var cbReading: CheckBox
    private lateinit var cbGame: CheckBox
    private lateinit var cbShopping: CheckBox
    private lateinit var btnSave: Button
    private lateinit var btnCancel: Button
    
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_edit_info)
        
        initViews()
        loadData()
        setListeners()
    }
    
    private fun initViews() {
        etName = findViewById(R.id.etName)
        etStudentId = findViewById(R.id.etStudentId)
        etPhone = findViewById(R.id.etPhone)
        etEmail = findViewById(R.id.etEmail)
        rgGender = findViewById(R.id.rgGender)
        rbMale = findViewById(R.id.rbMale)
        rbFemale = findViewById(R.id.rbFemale)
        cbTravel = findViewById(R.id.cbTravel)
        cbReading = findViewById(R.id.cbReading)
        cbGame = findViewById(R.id.cbGame)
        cbShopping = findViewById(R.id.cbShopping)
        btnSave = findViewById(R.id.btnSave)
        btnCancel = findViewById(R.id.btnCancel)
    }
    
    private fun loadData() {
        val name = intent.getStringExtra(MainActivity.EXTRA_NAME) ?: ""
        val gender = intent.getStringExtra(MainActivity.EXTRA_GENDER) ?: getString(R.string.radio_male)
        val studentId = intent.getStringExtra(MainActivity.EXTRA_STUDENT_ID) ?: ""
        val phone = intent.getStringExtra(MainActivity.EXTRA_PHONE) ?: ""
        val email = intent.getStringExtra(MainActivity.EXTRA_EMAIL) ?: ""
        val hobby = intent.getStringExtra(MainActivity.EXTRA_HOBBY) ?: ""
        
        etName.setText(name)
        etStudentId.setText(studentId)
        etPhone.setText(phone)
        etEmail.setText(email)
        
        // 设置性别
        if (gender == getString(R.string.radio_male)) {
            rbMale.isChecked = true
        } else {
            rbFemale.isChecked = true
        }
        
        // 设置爱好
        val hobbies = hobby.split(",")
        cbTravel.isChecked = hobbies.contains(getString(R.string.hobby_travel))
        cbReading.isChecked = hobbies.contains(getString(R.string.hobby_reading))
        cbGame.isChecked = hobbies.contains(getString(R.string.hobby_game))
        cbShopping.isChecked = hobbies.contains(getString(R.string.hobby_shopping))
    }
    
    private fun setListeners() {
        btnSave.setOnClickListener {
            saveData()
        }
        
        btnCancel.setOnClickListener {
            finish()
        }
    }
    
    private fun saveData() {
        val name = etName.text.toString()
        val studentId = etStudentId.text.toString()
        val phone = etPhone.text.toString()
        val email = etEmail.text.toString()
        
        val gender = if (rbMale.isChecked) {
            getString(R.string.radio_male)
        } else {
            getString(R.string.radio_female)
        }
        
        val hobbies = mutableListOf<String>()
        if (cbTravel.isChecked) hobbies.add(getString(R.string.hobby_travel))
        if (cbReading.isChecked) hobbies.add(getString(R.string.hobby_reading))
        if (cbGame.isChecked) hobbies.add(getString(R.string.hobby_game))
        if (cbShopping.isChecked) hobbies.add(getString(R.string.hobby_shopping))
        
        val hobby = hobbies.joinToString(",")
        
        val resultIntent = Intent().apply {
            putExtra(MainActivity.EXTRA_NAME, name)
            putExtra(MainActivity.EXTRA_GENDER, gender)
            putExtra(MainActivity.EXTRA_STUDENT_ID, studentId)
            putExtra(MainActivity.EXTRA_PHONE, phone)
            putExtra(MainActivity.EXTRA_EMAIL, email)
            putExtra(MainActivity.EXTRA_HOBBY, hobby)
        }
        
        setResult(RESULT_OK, resultIntent)
        finish()
    }
}

